<script>
import { unique } from './util'

export default {
  name: 'UiFilterItem',
  inject: ['UiFilter'],
  props: {
    label: {
      type: String
    },
    prop: {
      type: String
    },
    operater: {
      type: String,
      default: 'eq',
      validator (value) {
        return ['eq', 'in', 'like', 'between'].includes(value)
      }
    },
    more: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      info: {
        count: 0
      }
    }
  },
  computed: {
    props () {
      return {
        label: this.label,
        prop: this.prop,
        operater: this.operater,
        more: this.more,
        transform: (evt) => {
          if (this.transform) {
            return this.transform(evt)
          }
          let childComponent = null
          let childComonentProps = null
          if (this.$slots.default && this.$slots.default.length) {
            if (this.$slots.default[0].componentOptions && this.$slots.default[0].componentOptions.Ctor) {
              childComponent = this.$slots.default[0].componentOptions.Ctor.options
              childComonentProps = this.$slots.default[0].componentOptions.propsData
            }
          }
          if (childComponent && childComponent.filter && childComponent.filter.transformValue) {
            return childComponent.filter.transformValue({
              value: evt.value,
              props: childComonentProps
            })
          }
          return evt.value
        }
      }
    },
    item() {
      return {
        id: this.id,
        props: this.props,
        info: this.info,
        transform () {
          // // 如果在过滤组件中使用，并且不是带时间范围选择，那么要将起始和截止时间处理一下
          // if (this.UiFilter && this.type !== 'datetimerange') {
          //   val = val.map((v, i) => {
          //     const date = parseDate(v)
          //     if (i === 0) {
          //       date.setHours(0)
          //       date.setMinutes(0)
          //       date.setSeconds(0)
          //       date.setMilliseconds(0)
          //     } else {
          //       date.setDate(date.getDate() + 1)
          //       date.setHours(0)
          //       date.setMinutes(0)
          //       date.setSeconds(0)
          //       date.setMilliseconds(0)
          //     }
          //     return date.getTime() / 1000
          //   })
          // } else {
        },
        render: () => {
          return this.$scopedSlots.default()
        }
      }
    }
  },
  created() {
    this.id = unique()
    this.UiFilter.addItem(this.item)
  },
  beforeDestroy() {
    this.UiFilter.removeItem(this.id)
  },
  updated() {
    this.info.count++
  },
  render() {
    return null
  }
}
</script>
